<?php
	Header('Cache-Control: no-cache');
	Header('Pragma: no-cache');
	Header('X-UA-Compatible: IE=9');
	
	session_start();
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Einstellungen</title>
	<meta name="viewport"
        content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta charset="UTF-8">
	<link rel="icon" href="icons/favicon.ico" type="image/x-icon">
    <link rel="stylesheet" type="text/css" href="css/style.css">
  </head>
  <body>
	<?php 
		require_once 'navbar.php';
		
		if (!isset($_SESSION['userid']))
		{
			$db->showMsg(false,"");
			exit;
		}
		
		// Options fuer waehlbare Typen
		$typeIds = $db->getWhereaboutTypeSettings();
		
		// Aenderungen Speichern
		if (isset($_POST['save']) && $_POST['save'] == 1) {
			$error = false;
		
			while ($typeid = $typeIds->fetch_assoc()) {
				$id = $typeid['typeid'];
				
				if (isset($_POST['wt_name_'.$id]) && isset($_POST['wt_icon_'.$id]) &&
					isset($_POST['wt_agingPeriod_'.$id]) && isset($_POST['wt_agingTo_'.$id])) {
					
					// Periode ist in Stunden, DB braucht Sekunden
					$period = $_POST['wt_agingPeriod_'.$id] * 60 * 60 * 24;
					
					if ($db->setWhereaboutTypeSettings($id, $_POST['wt_name_'.$id],
						$_POST['wt_icon_'.$id], $period, 
						$_POST['wt_agingTo_'.$id]) == false) {
					
						$error = true;
						break;
					}
				}
			}
			
			$db->showMsg(!$error, "UPDATE");
		}
		
		// Typen aktualisieren
		$typeIds = $db->getWhereaboutTypeSettings();
		
		// Whereaboutsettings holen
		$settings = $db->getWhereaboutTypeSettings();
		
		// Options fuer waehlbare Icons erstellen
		$dir = "./icons/markers";
		$files = scandir($dir);
		$index = 0;
		foreach ($files as $file) {
			if (preg_match('/.png$/i', $file)) {
				$iconOptions[$index] = $file;
				$index++;
			}
		}
	?>

	<form name="save" action="settings.php" method="post">
		<input type="hidden" name="save" value="1" />
		<div class="phonelists">
			<table>
				<tr>
					<th>Name</th>
					<th>Symbol</th>
					<th>Typ&auml;nderung nach Tagen<br />(0 f&uuml;r keine &Auml;nderung)</th>
					<th>Neuer Typ</th>
				</tr>
				<?php while ($row = $settings->fetch_assoc()) : ?>
				<tr>
					<td>
						<input type="text" name="wt_name_<?php echo $row['typeid'] ?>" value="<?php echo $row['name'] ?>" />
					</td>
					<td>
						<select size="1" name="wt_icon_<?php echo $row['typeid'] ?>" 
						onchange="document.getElementById('wt_img_<?php echo $row['typeid'] ?>').src=this.options[this.selectedIndex].value">
							<?php
							foreach($iconOptions as $option) {
								echo '<option value="'.$dir.'/'.$option.'"';
								
								// Ausgewaehlt?
								if ($dir.'/'.$option == $row['icon']) {
									echo ' selected="selected"';
								}
								echo '>'.$option.'</option>';
							}
							?>
						</select>
						<img id="wt_img_<?php echo $row['typeid'] ?>" src="<?php echo $row['icon'] ?>" alt="markerIcon" />
					</td>
					<td>
						<input type="text" name="wt_agingPeriod_<?php echo $row['typeid'] ?>" 
						value="<?php echo ($row['agingPeriod'] / (60*60*24)) ?>" />
					</td>
					<td>
						<select name="wt_agingTo_<?php echo $row['typeid'] ?>" size="1">
							<?php
							$typeIds->data_seek(0);
							while ($typeid = $typeIds->fetch_assoc()) {
								echo '<option value="'.$typeid['typeid'].'"';
								
								// Ausgewaehlt?
								if ($row['agingToTypeId'] == $typeid['typeid']) {
									echo ' selected="selected"';
								}
								echo '>'.$typeid['name'].'</option>';
							}
							?>
						</select>
					</td>
				</tr>
				<?php endwhile; ?>
			</table>
		</div>
		<div class="login">
			<input type="submit" value="Speichern" />
		</div>
	</form>
  </body>
</html>
